package com.gzhu.knowledgeAdmin.controller;


import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.utils.GoFastDfsUtil;
import com.gzhu.knowledgeAdmin.common.vo.GoFastDfsUploadFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping
public class AudioController {

    private final String audioPath = "audioFiles";

    @Autowired
    private GoFastDfsUtil goFastDfsUtil;

//    @GetMapping("/audio/{recorder:.+}/{type:.+}")
//    @ResponseBody
//    public ResponseEntity<Resource> getAudioFile(@PathVariable String recorder, @PathVariable String type) {
//        byte[] fileBytes = goFastDfsUtil.getFileBytes("http://localhost:8080/audioFiles/InTheEnd万人合唱版LinkinPark.mp3");
//        Resource audioFile = new ByteArrayResource(fileBytes);
//
//        if (!audioFile.exists()) {
//            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"音频文件不存在");
//        }
//
//        return ResponseEntity.ok()
//                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + audioFile.getFilename() + "\"")
//                .contentType(MediaType.parseMediaType("audio/mpeg"))
//                .body(audioFile);
//    }

    /**
     * 上传音频
     * @param audio
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @RequestMapping("/audio/uploadAudio")
    public R uploadFile(@RequestPart("file") MultipartFile audio) throws Exception{
        GoFastDfsUploadFileVo goFastDfsUploadFileVo = goFastDfsUtil.uploadFile(audio, audioPath);

        return R.ok(goFastDfsUploadFileVo.getUrl());
    }


}
